'''
Created on 2014-4-25

@author: shen
'''
from fetch_info_to_file.Review_Stuff import Review_Stuff
from read_list_from_file.read_feature_list import Get_Feature_List


#from matplotlib.mlab import load
import numpy as np
from matplotlib.pyplot import plot
import matplotlib.pyplot as plt
from read_list_from_file.read_price_list import Get_Price_List

def get_ASIN_and_review_num():
    review = Review_Stuff()
    feature = Get_Feature_List()
    review_list = review.readFromLocal('Review_Stuff')
    feature_num_list = feature.get_infolist()
    price_list = Get_Price_List().get_infolist()
    
    pure_price_list = []
    
    for item in price_list:
        pure_price_list.append(item['price'])
    
    
    min_price = min(pure_price_list)
    max_price = max(pure_price_list)
    print max_price,min_price
    
    list=[]
    
    for item in review_list:
        ASIN = item['ASIN']
        review_num = item['review_count']
        feature_num=0
        ifFound=False
        for item2 in feature_num_list:
            if cmp(ASIN,item2['ASIN'])==1:
#                print item2['ASIN']
                feature_num = len(item2['feature'])
                ifFound = True
                break
        if ifFound==True:  
            list.append({'ASIN':ASIN , 'review_count':review_num , 'feature_count':feature_num})
    return list
    
    
    """
When plotting daily data, a frequent request is to plot the data
ignoring skips, eg no extra spaces for weekends.  This is particularly
common in financial time series, when you may have data for M-F and
not Sat, Sun and you don't want gaps in the x axis.  The approach is
to simply use the integer index for the xdata and a custom tick
Formatter to get the appropriate date string for a given index.
"""
def draw():
    list = get_ASIN_and_review_num()
    count = [0,0,0,0,0,0,0]
    review_count = [0,0,0,0,0,0,0]
    for item in list:
        feature_count = item['feature_count']
        i=0
        if feature_count<6:
            i=feature_count
        else:
            i=6
        count[i]=count[i]+1
        review_count[i]=review_count[i]+item['review_count']
    
    average_count=[]
    for i in range(0,7):
        if count[i]==0:
            print i
            average_count.append(0.0)
        else:
            average_count.append(review_count[i]/count[i])
        
#    x_list = [1,2,3,4,5,6,7]
    x_list = ["0 feature\n"+str(count[0]),"1 feature\n"+str(count[1]),"2 feature\n"+str(count[2]),"3 feature\n"+str(count[3]),"4 feature\n"+str(count[4]),"5 feature\n"+str(count[5]),">5 feature\n"+str(count[6])]
    x_pos = np.arange(len(x_list))
    
    plt.bar(x_pos, average_count , align='center')
    plt.xticks(x_pos,x_list)
    plt.title('average sales volume of different feature count')
    
    plt.show()
    
if __name__ == '__main__':
    draw()